<?php
//get the q parameter from URL
$q=$_GET["q"];

//find out which feed was selected
if($q=="Google")
  {
  $xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
  }
elseif($q=="MSNBC")
  {
  $xml=("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml");
  }
elseif($q=="Newsit")
  {
  $xml=("http://www.newsit.gr/rss/artrss.php");
  }  
  
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

/*
//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;

//output elements from "<channel>"
echo("<p><a href='" . $channel_link
  . "'>" . $channel_title . "</a>");
echo("<br />");
echo($channel_desc . "</p>");
*/

//Connect with database and select table
$link = mysql_connect('localhost', 'mysql_user', '');
if (!$link)
{
    die('Could not connect: ' . mysql_error());
}
$set_charset_err = mysql_set_charset('utf8',$link);

//mysql_query('SELECT * FROM test.RSS');
$db_selected = mysql_select_db('test', $link);
if (!$db_selected)
{
    die ('Can\'t use test : ' . mysql_error());
}

//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=2; $i++)
  {
  $item_pubDate=$x->item($i)->getElementsByTagName('pubDate')
  ->item(0)->childNodes->item(0)->nodeValue;
  //$item_category=$x->item($i)->getElementsByTagName('category')
  //->item(0)->childNodes->item(0)->nodeValue;    
  $item_title=$x->item($i)->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_link=$x->item($i)->getElementsByTagName('link')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_desc=$x->item($i)->getElementsByTagName('description')
  ->item(0)->childNodes->item(0)->nodeValue;
 
	//add the data to the database, I hope  
	$sql = 'INSERT INTO RSS (PUBDATE, TITLE, LINK, DESCRIPTION) VALUES ("'.$item_pubDate.'","'.$item_title.'","'.$item_link.'","'.$item_desc.'")';
	if (mysql_query($sql, $link))
	{
		echo "Added data successfully\n";
	}
	else
	{
		echo 'Error adding data: ' . mysql_error() . "\n";
		echo 'SQL query: ' . $sql . "\n";
	}  

/*	
  //echo the item
  echo ("<p><br /><a href='" . $item_link
  . "'>" . $item_title . "</a> ".$item_pubDate);
  echo ("<br />");
  echo ($item_desc . "</p>");
*/
}

$rss_resource = mysql_query("SELECT PUBDATE, TITLE, LINK, DESCRIPTION FROM RSS");
$num_rows = mysql_num_rows($rss_resource);

for ($i=0; $i<=$num_rows; $i++)
{
	echo 'Item '.$i;
	$rss_item = mysql_fetch_row($rss_resource);

	echo ("<p><br /><a href='" . $rss_item[2]
	. "'>" . $rss_item[1] . "</a> ".$rss_item[0]/*." ".$item_category*/);
	echo ("<br />");
	echo ($item_desc . "</p>");
}
?> 